//---------------------------------------------------------------------------

#ifndef CBitBtnH
#define CBitBtnH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Buttons.hpp>
#include <memory>
//---------------------------------------------------------------------------
// Pour la différence entre la couleur donnée et celle qui sera retournée
//---------------------------------------------------------------------------
#define BTN_COL_PAS_DEFAULT_LOW 0x40
#define BTN_COL_PAS_DEFAULT_HIGH 0x40
//---------------------------------------------------------------------------
class CSpeedButton;
class BitBtnOption
{
public:
  char *Name;
  TModalResult ModalResult;
  char *Caption;
};

namespace Cbitbtn{
enum CBitBtnKind
{
  bkCustom,
  bkOK,
  bkCancel,
  bkHelp,
  bkYes,
  bkNo,
  bkClose,
  bkAbort,
  bkRetry,
  bkIgnore,
  bkAll,
  bkAdd,
  bkModify,
  bkDelete,
  bkDuplicate,
  bkDetail,
  bkPrint,
  bkPreview,
  bkImport,
  bkExport,
  bkNext,
  bkPrior,
  bkUp,
  bkDown,
  bkSave,
  bkVisa,
  bkCacher,
  bkLock,
  bkUnlock,
  bkRestore,
  bkParcourir,
  bkeMail,
  bkToutSelectionner,
  bkInverserSelection,
  bkToutDeselectionner,
  bkSupprimerTout,
  bkLettrer,
  bkDelettrer,
  bkPrelettrer,
  bkSelectionner,
  bkDeselectionner,
  bkAdd_2,
  bkCalculAuto,
  bkPDF,
  bkRemplacer,
  bk3Points,
  bkMoveUp,
  bkMoveDown,
  bkRechercher,
  bkEffacer,
  bkRafraichir,
};

#define NB_KIND 52

class PACKAGE CBitBtn : public TButton
{
    friend class CSpeedButton;
__published:
    __property TColor Color = {read = GetColor, write = SetColor/*, default = clBtnFace*/};
    __property Graphics::TBitmap *Glyph = {read = m_Glyph, write = SetGlyph, stored = IsCustom};
    __property CBitBtnKind Kind = {read = FKind, write = SetKind, default=0};
    __property int Margin = {read = m_Margin, write = SetMargin, default = -1};
    __property int Spacing = {read = m_Spacing, write = SetSpacing, default = 4};

protected:
    __property TCanvas* Canvas = {read = GetCanvas};

    // fonctions héritées
    virtual void __fastcall CreateParams(TCreateParams& Params)
      {
        TButton::CreateParams(Params);
        Params.Style |= BS_OWNERDRAW;
      }
    virtual void __fastcall SetButtonStyle(bool ADefault);

private:
    std::auto_ptr<TCanvas> Canvas_;
    bool draw_as_default_;
    static void __fastcall InitListeGlyphs();
    static void __fastcall DeInitListeGlyphs();
    static int NbCBitBtn_;
    CBitBtnKind FKind;
    Graphics::TBitmap* m_Glyph;
    int m_Margin;
    int m_Spacing;

    TCanvas* __fastcall GetCanvas()
      {
        return Canvas_.get();
      }
    TColor __fastcall GetColor()
      {
        return Brush->Color;
      }
    CBitBtnKind __fastcall GetKind(void);

    void __fastcall SetColor(TColor Value)
      {
        if (Brush->Color != Value)
        {
          Brush->Color = Value;
          InvalidateRect(Handle, NULL, FALSE);
        }
      }
    void __fastcall SetKind(CBitBtnKind Value);

    MESSAGE void __fastcall CNDrawItem(TMessage& Msg);
    MESSAGE void __fastcall WMLButtonDblClk(TMessage& Msg);
    MESSAGE void __fastcall CMFontChanged(TMessage& Msg);
    MESSAGE void __fastcall CMEnabledChanged(TMessage& Msg);
    MESSAGE void __fastcall WMEraseBkgnd(TMessage& Msg);

    void __fastcall SetGlyph(Graphics::TBitmap* Value);
    bool __fastcall IsCustom();
    void __fastcall SetMargin(int iMargin);
    void __fastcall SetSpacing(int iSpacing);

public:
    BEGIN_MESSAGE_MAP
        MESSAGE_HANDLER(CN_DRAWITEM, TMessage, CNDrawItem)
        MESSAGE_HANDLER(WM_LBUTTONDBLCLK, TMessage, WMLButtonDblClk)
        MESSAGE_HANDLER(CM_FONTCHANGED, TMessage, CMFontChanged)
        MESSAGE_HANDLER(CM_ENABLEDCHANGED, TMessage, CMEnabledChanged)
        MESSAGE_HANDLER(WM_ERASEBKGND, TMessage, WMEraseBkgnd)
    END_MESSAGE_MAP(TButton)
    __fastcall CBitBtn(TComponent* Owner);
    __fastcall ~CBitBtn();
};

} // namespace Cbitbtn
#ifndef NO_IMPLICIT_NAMESPACE_USE
 using namespace Cbitbtn;
#endif

TColor __fastcall GetColorLo(int _nColor, int _nPasParCouleur = BTN_COL_PAS_DEFAULT_LOW);
TColor __fastcall GetColorHi(int _nColor, int _nPasParCouleur = BTN_COL_PAS_DEFAULT_HIGH);
void __fastcall DoDrawButtonFace(TCanvas* Canvas,
                                 const TRect &ClientRect,
                                 bool bFlatMode,
                                 bool bTransparent,
                                 TButtonState State,
                                 bool bMouseInControl,
                                 TColor Color,
                                 bool  bDessinParDefaut,
                                 const TOwnerDrawState& state,
                                 bool bDown);
void __fastcall DoDrawButtonText(TCanvas* Canvas,
                                 const TRect &ClientRect,
                                 TColor Color,
                                 TColor FontColor,
                                 const AnsiString &aCaption,
                                 CBitBtnKind Kind,
                                 Graphics::TBitmap *Glyph,
                                 int Margin,
                                 int Spacing,
                                 const TOwnerDrawState& state);
TPoint __fastcall GetPosGlyph(TCanvas* Canvas,
                              const TRect &ClientRect,
                              const AnsiString &aCaption,
                              Graphics::TBitmap* LeGlyph,
                              int Margin,
                              int Spacing,
                              const TOwnerDrawState& state);
void __fastcall DoDrawButtonGlyph(TCanvas* Canvas,
                                  const TRect &ClientRect,
                                  TColor Color,
                                  const AnsiString &aCaption,
                                  CBitBtnKind Kind,
                                  Graphics::TBitmap *Glyph,
                                  int Margin,
                                  int Spacing,
                                  const TOwnerDrawState& state);

//---------------------------------------------------------------------------
#endif
